import {
	deepClone
} from "../utils/liJsonUtils";
import {
	uHideLoading,
	uSetNavigationBarTitle,
	uShowLoading,
	uSwitchTab,
	utoast,
} from "../utils/uniUtils";

export default {
	data() {
		return {
			tableData: [],
			navTitle: "",
			isLoaded: false,
			summaryObj: {},
			tabIdx: 0,
			isShowLoadMore: false,
			total: 0,
			isShow: false,
		};
	},
	async onLoad() {
		try {
			uShowLoading();
			await this.getPageData();
		} catch (error) {
			console.log("error", error);
			uHideLoading();
			utoast("页面初始化失败！", "error");
			setTimeout(() => {
				uSwitchTab("/pages/tabBar/reportForms");
			}, 1000);
		} finally {
			this.isLoaded = true;
			uHideLoading();

			setTimeout(() => {
				this.isShow = true
			})
		}
	},
	onShow() {
		if (this.isShow && this.Bill) {
			this.getPageData()
		}
	},
	mounted() {
		if (this.navTitle && this.navTitle.trim().length != 0) {
			uSetNavigationBarTitle(this.navTitle.trim());
		}
	},
	methods: {
		async getPageData() {
			const res =
				this.reportFn instanceof Function ?
				await this.reportFn(this.param) :
				await this.$api[this.reportFn](this.param);
			if (res.data.list) {
				if (this.isShowLoadMore) {
					this.tableData = [...deepClone(this.tableData), ...deepClone(res.data.list)];
				} else {
					this.tableData = res.data.list;
				}

				this.total = res.data.total;
			}

			if (res.data.summaryObj) {
				this.summaryObj = res.data?.summaryObj;
			}
		},
		async onResetFields(e) {
			this.param = this.$options.data().param;
			await this.getPageData();
			utoast("重置成功！");
		},
		// methods put on the top
	},
};